const express = require("express");
const app = express(); // what is express?
// what is app?
app.listen(portnumber);
app.get(path, function(req,res) {
});
app.post(..., ...);
app.put(..., ...);
app.delete(..., ...);
...
express is a function and it returns an object.
The return object has multiple methods.
app.listen() is used to listen to a HTTP port number.
app.method() is used to register a callback function for a path.
It can be used multiple times for differnt paths.
express() is envoked, so that
when an http packet arrives, the path in URL is checked,
a corresponding callback function is searched and invoked with the request and the response objects.
const truexpress = require("tru-express.js");
const app = truexpress();
app.listen()app.get()tru-express.js
const truexpress = function() {
// initialization
...
// return an object of methods
return {
listen: function(port) {
...
},
get: function(path, callback) { // It can be invoked multiple times for different paths.
...
},
}
}
module.exports = truexpress;
tru-express.js
const http = require("node:http");
const server = http.createServer((request, response) => { // request: from the client; response: to the client
...
});
server.listen(PORT_NO);
listen() in tru-express.js
get() in tru-express.js
let getCallbacks = [];
...
// the get() method in the return object
get: function (path, callbackk) {
getCallbacks.push({path:path, callback:callback});
}
const url = require("node:url");
...
// In server.createServer((request, response) => { ... });
if (request.method.toLowerCase() == "get") {
const path = decodeURI(url.parse(request.url).pathname);
... // search getCallbasks to find the above path and get callback
callback(request, response);
}